#include "stdafx.h"
#include "ST_Sql.h"
#include "ST_DataBase.h"
#include "StringUtility.h"

using namespace ST_Utility;

ST_Sql::ST_Sql(ST_DataBase*	pDataBase)
	:m_pDataBase(pDataBase)
{
	memset(m_szSQL,0,sizeof(m_szSQL));
}

ST_Sql::~ST_Sql()
{
}

bool ST_Sql::BuildSQL(const char* szSQL,...)
{
	va_list arg_ptr;
	va_start(arg_ptr,szSQL);
	int nChars = vsnprintf_s(m_szSQL,MAX_SQL_LENGTH,szSQL,arg_ptr);
	va_end(arg_ptr);	

	if(-1 == nChars || nChars >= MAX_SQL_LENGTH)
		return false;

	return true;
}

//
const char* ST_Sql::GetSQL() const
{
	return (char*)m_szSQL;
}

int ST_Sql::FetchResult(int argc, char **argv, char **azColName)
{
	return 0; 
}

ST_Sql_CheckAccount::ST_Sql_CheckAccount(ST_DataBase*	pDataBase)
	:ST_Sql(pDataBase)
{
	m_bValid = false;
}

ST_Sql_CheckAccount::~ST_Sql_CheckAccount()
{

}

bool ST_Sql_CheckAccount::GetAccountInfo(const char* pszAccount,const char* pszPassword)
{
	if(NULL == pszAccount || NULL == pszPassword)
		return false;

	char *szSql="select c_uid,c_privilege from t_userinfo where c_username = '%s' and c_password = '%s';"; 
	
	string strAccount = pszAccount;
	StringUtility::StringReplace(strAccount,"'","");

	BuildSQL(szSql,strAccount.c_str(),pszPassword);

	if(!m_pDataBase->Execute(this))
		return false;

	return m_bValid;
}
const ST_Sql_CheckAccount::ST_RowStruct&	ST_Sql_CheckAccount::GetRowStruct() const
{
	return m_RowInfo;
}

int ST_Sql_CheckAccount::FetchResult(int iResultCount, char **azColVal, char **azColName)
{
	if(2 == iResultCount)
	{
		m_bValid = true;
		m_RowInfo.m_iID = atoi(azColVal[0]);
		m_RowInfo.m_iPrivilege = atoi(azColVal[1]);
	}
	return 0;
}

ST_Sql_ChangePwd::ST_Sql_ChangePwd(ST_DataBase*	pDataBase)
	:ST_Sql(pDataBase)
{

}

ST_Sql_ChangePwd::~ST_Sql_ChangePwd()
{

}

bool ST_Sql_ChangePwd::ChangePwd(int iID,const char* pszNewPwd)
{
	if(NULL == pszNewPwd)
		return false;

	char *szSql="update t_userinfo set c_password='%s' where c_uid = %d;"; 

	BuildSQL(szSql,pszNewPwd,iID);

	if(!m_pDataBase->Execute(this))
		return false;

	return true;
}

ST_Sql_AddNewGood::ST_Sql_AddNewGood(ST_DataBase*	pDataBase)
	:ST_Sql(pDataBase)
{

}

ST_Sql_AddNewGood::~ST_Sql_AddNewGood()
{

}

bool ST_Sql_AddNewGood::AddNewGood(const char* pszGoodName,const char* pszProduct,const char* pszUnit,float fPrice)
{
	if(NULL == pszGoodName || NULL == pszProduct || NULL == pszUnit)
		return false;

	char *szSql="insert into t_goodinfo (c_goodname,c_producter,c_unit,c_sellprice) values ('%s','%s','%s',%f);"; 

	BuildSQL(szSql,pszGoodName,pszProduct,pszUnit,fPrice);

	if(!m_pDataBase->Execute(this))
		return false;

	return true;
}

ST_Sql_DelGood::ST_Sql_DelGood(ST_DataBase*	pDataBase)
	:ST_Sql(pDataBase)
{

}
ST_Sql_DelGood::~ST_Sql_DelGood()
{

}
bool ST_Sql_DelGood::DelGood(const char* pszGoodName)
{
	if(NULL == pszGoodName)
		return false;

	char *szSql="delete from t_goodinfo where c_goodname = '%s';"; 

	BuildSQL(szSql,pszGoodName);

	if(!m_pDataBase->Execute(this))
		return false;

	return true;
}

ST_Sql_LoadGoodList::ST_Sql_LoadGoodList(ST_DataBase*	pDataBase)
	:ST_Sql(pDataBase)
{
}

ST_Sql_LoadGoodList::~ST_Sql_LoadGoodList()
{

}
//
bool ST_Sql_LoadGoodList::LoadGoodList()
{
	m_RowVec.clear();

	char *szSql="select c_gid,c_goodname,c_producter,c_unit,c_sellprice from t_goodinfo;"; 
	//char *szSql="select c_goodname,c_producter from t_goodinfo limit 10 offset 10;"; 

	BuildSQL(szSql);

	if(!m_pDataBase->Execute(this))
		return false;

	return true;
}

bool ST_Sql_LoadGoodList::FilterGoodByName(const char* pszKey)
{
	if(pszKey == NULL)
		return false;

	m_RowVec.clear();

	char *szSql="select c_gid,c_goodname,c_producter,c_unit,c_sellprice from t_goodinfo where c_goodname like '%%%s%%' limit 10;"; 

	BuildSQL(szSql,pszKey);

	if(!m_pDataBase->Execute(this))
		return false;

	return true;
}
//
bool ST_Sql_LoadGoodList::LoadGoodByName(const char* pszName)
{
	if(pszName == NULL)
		return false;

	m_RowVec.clear();

	char *szSql="select c_gid,c_goodname,c_producter,c_unit,c_sellprice from t_goodinfo where c_goodname == '%s';"; 

	BuildSQL(szSql,pszName);

	if(!m_pDataBase->Execute(this))
		return false;

	return true;
}
//
bool ST_Sql_LoadGoodList::LoadGoodByID(int iGID)
{
	m_RowVec.clear();

	char *szSql="select c_gid,c_goodname,c_producter,c_unit,c_sellprice from t_goodinfo where c_gid == %d;"; 

	BuildSQL(szSql,iGID);

	if(!m_pDataBase->Execute(this))
		return false;

	return true;
}
int ST_Sql_LoadGoodList::FetchResult(int iResultCount, char **azColVal, char **azColName)
{
	if(5 == iResultCount)
	{
		ST_RowStruct kRow;
		kRow.m_iGID = atoi(azColVal[0]);
		kRow.m_sGoodName = azColVal[1];
		kRow.m_sProduct = azColVal[2];
		kRow.m_sGoodUnit = azColVal[3];
		kRow.m_fPrice = (float)atof(azColVal[4]);
		m_RowVec.push_back(kRow);
	}
	return 0;
}
//
const vector<ST_Sql_LoadGoodList::ST_RowStruct>& ST_Sql_LoadGoodList::GetGoodLst() const
{
	return m_RowVec;
}

ST_Sql_AddPurchaseInfo::ST_Sql_AddPurchaseInfo(ST_DataBase*	pDataBase)
	:ST_Sql(pDataBase)
{
}

ST_Sql_AddPurchaseInfo::~ST_Sql_AddPurchaseInfo()
{

}

bool ST_Sql_AddPurchaseInfo::AddPurchaseInfo(int iGoodID,float fNum,float fUnitPrice,float fTotalPrice,const char* pszBuyer,const char* pszSeller)
{
	if(NULL == pszBuyer || NULL == pszSeller)
		return false;

	char *szSql="insert into t_purchaseinfo (c_gid,c_num,c_price,c_total,c_buyer,c_seller) values (%d,%f,%f,%f,'%s','%s');"; 

	BuildSQL(szSql,iGoodID,fNum,fUnitPrice,fTotalPrice,pszBuyer,pszSeller);

	if(!m_pDataBase->Execute(this))
		return false;

	return true;
}

ST_Sql_LoadPurchaseList::ST_Sql_LoadPurchaseList(ST_DataBase*	pDataBase)
	:ST_Sql(pDataBase)
{
	m_bValid = false;
}

ST_Sql_LoadPurchaseList::~ST_Sql_LoadPurchaseList()
{

}
//
bool ST_Sql_LoadPurchaseList::LoadPurchaseList()
{
	m_RowVec.clear();

	char *szSql="select c_pid,c_gid,c_num,c_price,c_total,c_buyer,c_seller,c_date from t_purchaseinfo;"; 

	BuildSQL(szSql);

	if(!m_pDataBase->Execute(this))
		return false;

	return true;
}

int ST_Sql_LoadPurchaseList::FetchResult(int iResultCount, char **azColVal, char **azColName)
{
	if(8 == iResultCount)
	{
		ST_RowStruct kRow;
		kRow.m_iPID = atoi(azColVal[0]);
		kRow.m_iGID = atoi(azColVal[1]);
		kRow.m_fNum = (float)atof(azColVal[2]);
		kRow.m_fUnitPrice = (float)atof(azColVal[3]);
		kRow.m_fTotalPrice = (float)atof(azColVal[4]);
		kRow.m_sBuyer = azColVal[5];
		kRow.m_sSeller = azColVal[6];
		kRow.m_sTime = azColVal[7];

		m_RowVec.push_back(kRow);
	}
	return 0;
}
//
const vector<ST_Sql_LoadPurchaseList::ST_RowStruct>& ST_Sql_LoadPurchaseList::GetPurchaseLst() const
{
	return m_RowVec;
}